var interval;
var listMember;
var allData;
// var awardStatus = false;
var options;
var current_reward_id;
var current_reward_lever_result;
var current_reward_times = 1;
// var rewards;
var submit = $(".submit")

$(function () {
  console.log(new Date() + "：开始从后台数据");
  $.ajaxSettings.async = false;
  $.get(
    "/reward/getList",
    {},
    function (data) {
      console.log(new Date() + "：数据加载完毕");
      if (data) {
          allData = data;
          options = data.option;
      } else {
        alert("Ajax error!");
      }
    },
    "json"
  );

  // var html = "";
  // for(var i=0;i<options.length;i++){
  //   // console.log(options[i]["img_url"]);
  //   if(options[i].img_show_state==1){
  //     html+="<li class=\"reward-item\" >\n" +
  //         "        <div class=\"imgcontain\"><img src=\""+options[i]["img_url"]+"\" alt=\"\"></div>\n" +
  //         "    </li>";
  //   }
  // }
  // if (options.length >= 3){
  //   $("#reward-image").css("width","840px");
  //   $("#reward-image>ul").html(html);
  // } else {
  //   var width = 280*options.length;
  //   console.log("width:"+width);
  //   $("#reward-image").css("width",width + "px");
  //   $("#reward-image>ul").html(html);
  // }
});

//初始化页面
function initPage(){
  $("#xyyd").css("display","none");
  $("#reward-image").css("display","none");
  if (scrollTimmer != null){
    clearTimeout(scrollTimmer);
  }
  var str = '';
  var leverName;
  var arr;
    if (options.length >= current_reward_times) {
    leverName = options[current_reward_times-1]["name"];
    arr = leverName.split(/\d{1,}/);
    var num = parseInt(leverName.replace(/[^0-9]/ig,""));
    str = arr[0] + convertToChinaNum(num) + arr[1];
  }else{
    leverName = options[0]["name"];
    arr = leverName.split("1");
    console.log(arr);
    str=arr[0] + convertToChinaNum(current_reward_times) + arr[1];
  }
    current_reward_lever_result = str.substring(2,str.length) + "抽奖结果";
    // console.log("结果："+current_reward_lever_result);
    $('#controllBT').html(str);
  // }
}

//监听空格事件
$(document).keydown(function (event) {
  // console.log("键值：" + event.keyCode);
  if (event.keyCode == 32){
    console.log("random");
    $("#controllBT").css("display","block");
    randomData(36,".submit");
  }
});


//取中奖数据
function getData(id,leverName) {
  $.ajaxSettings.async = false;
  $.get(
      "/reward/getRatio/" + id,
      {},
      function (data) {
        if (data) {
          listMember = data[leverName];
        } else {
          alert("Ajax error!");
        }
      },
      "json"
  );

}


//显示中奖名单
function clos(id,lever) {
  clearInterval(interval);
  getData(id,lever);
  var res = listMember;
  var str = "<ul id='scrollul'>";
  for (o in res) {
    str +=
        '<li class="col-md-3" data-id="' +
        res[o].id +
        '">' +
        "<span>" +
        res[o].name +
        "</span><label>" +
        res[o].id +
        "</label></li>";
  }
  str += "</ul>";
  $(".prizeList").html(str);
  $(".submit").data("id", "0");
  // $(".submit").text("开始");
  scroll();
}

var scrollTimmer = null;
var scrollTop = 0;

function scroll() {
  $('#controllBT').html(current_reward_lever_result);
  var outheight = $(".prizeList").height();
  var ulHeight = $("#scrollul").height();

  scrollTop = outheight;
  if (ulHeight > outheight){
    $("#scrollul").css("top",scrollTop + "px");

    function scrollT() {
      if (scrollTop >= (-1)*ulHeight){
        // console.log("gao:" + outheight + ":" + ulHeight);
        // console.log(scrollTop + ":" +(-1)*ulHeight);
        scrollTop = scrollTop - 2;
      } else {
        scrollTop = outheight;
      }
      $("#scrollul").css("top",scrollTop + "px");
      console.log(scrollTop);
    }
    scrollTimmer = setInterval(scrollT,9);
  }

}

//循环遍历数据
function randomData(listnum, obj) {
  var status = $(obj).data("id");
  var ints;
  if (status == 0) {
    if (allData) {
      initPage();
      function times() {
        var ArrList = allData.data;
        var res = getArrayItems(ArrList, listnum);
        str = "";
        for (o in res) {
          num = parseInt(o) + 1;
          str +=
              '<li class="col-md-3" data-id="' + res[o].id + '">'
              + "<span>" + res[o].name + "</span><label>" + res[o].id + "</label></li>";
        }
        $("#main-countainer").css("display","block");
        $(".prizeList").html(str);
      }
      interval = setInterval(times, 10);
      $(".submit").data("id", "1");
      $(".submit").text("停止");

    } else {
      alert("Ajax error!");
    }
  } else {
    var leverName;
    if (options.length >= current_reward_times){
      leverName = options[current_reward_times-1]["name"];
    } else {
      leverName = options[0]["name"];
      var arr = leverName.split("1");
      leverName = arr[0] + current_reward_times + arr[1];
    }
    console.log("lever:" + leverName);
    clos(current_reward_times,leverName);
    current_reward_times++;

    // clearInterval(interval);
    $(".submit").data("id", "0");
    $(".submit").text("开始");
  }
}

//从一个给定的数组arr中,随机返回num个不重复项
function getArrayItems(arr, num) {
  $("#main-countainer").css("display", "block");
  //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组;
  var temp_array = new Array();
  for (var index in arr) {
    temp_array.push(arr[index]);
  }
  //取出的数值项,保存在此数组
  var return_array = new Array();
  for (var i = 0; i < num; i++) {
    //判断如果数组还有可以取出的元素,以防下标越界
    if (temp_array.length > 0) {
      //在数组中产生一个随机索引
      var arrIndex = Math.floor(Math.random() * temp_array.length);
      //将此随机索引的对应的数组元素值复制出来
      return_array[i] = temp_array[arrIndex];
      //然后删掉此索引的数组元素,这时候temp_array变为新的数组
      temp_array.splice(arrIndex, 1);
    } else {
      //数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项.
      break;
    }
  }
  return return_array;
}


function convertToChinaNum(num) {
  var arr1 = new Array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
  var arr2 = new Array('', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千','万', '十', '百', '千','亿');//可继续追加更高位转换值
  if(!num || isNaN(num)){
    return "零";
  }
  var english = num.toString().split("");
  var result = "";
  for (var i = 0; i < english.length; i++) {
    var des_i = english.length - 1 - i;//倒序排列设值
    result = arr2[i] + result;
    var arr1_index = english[des_i];
    result = arr1[arr1_index] + result;
  }
  //将【零千、零百】换成【零】 【十零】换成【十】
  result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十');
  //合并中间多个零为一个零
  result = result.replace(/零+/g, '零');
  //将【零亿】换成【亿】【零万】换成【万】
  result = result.replace(/零亿/g, '亿').replace(/零万/g, '万');
  //将【亿万】换成【亿】
  result = result.replace(/亿万/g, '亿');
  //移除末尾的零
  result = result.replace(/零+$/, '');
  //将【零一十】换成【零十】
  //result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十
  //将【一十】换成【十】
  result = result.replace(/^一十/g, '十');
  return result;
}